package com.zerosky.crm.controller;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zerosky.crm.po.Branch;
import com.zerosky.crm.po.ChartSales;
import com.zerosky.crm.po.CustomerGrowth;
import com.zerosky.crm.po.Department;
import com.zerosky.crm.po.Employee;
import com.zerosky.crm.po.Objective;
import com.zerosky.crm.po.Pluralize;
import com.zerosky.crm.service.BranchService;
import com.zerosky.crm.service.ChartService;
import com.zerosky.crm.service.DepartmentService;
import com.zerosky.crm.service.ObjectiveService;
import com.zerosky.crm.service.PluralizeService;
import com.zerosky.crm.util.BaseAction;

/**
 * 统计图表Controller
 * 
 * @author 钟璟
 *
 */
@Controller
public class ChartController extends BaseAction {
	private Logger logger = LoggerFactory.getLogger(ChartController.class); // 日志

	@Resource(name = "chartService")
	private ChartService chartService;

	@Resource(name = "departmentService")
	private DepartmentService departmentService;

	@Resource(name = "branchService")
	private BranchService branchService;

	@Resource(name = "objectiveService")
	private ObjectiveService objectiveService;

	@Resource(name = "pluralizeService")
	private PluralizeService pluralizeService;

	@RequestMapping("getMarketingObjectives.do")
	@ResponseBody
	public Map<String, Object> getMarketingObjectives(HttpServletRequest request, HttpSession session) {
		Map<String, Object> json = new HashMap<String, Object>(); // 定义返回map集合
		Employee user = (Employee) session.getAttribute("loginUser");// 获取登录用户信息
		Map<String, Object> map = new HashMap<String, Object>(); // 定义查询条件map集合
		try {
			map.put("employeeId", user.getEmployeeId());// 获取用户ID
			List<String> chartSales = new ArrayList<String>();
			List<String> objective = new ArrayList<String>();
			Calendar cal = Calendar.getInstance();
			int year = cal.get(Calendar.YEAR);
			map.put("year", year);// 获取年份
			for (int i = 1; i <= 12; i++) {
				map.put("month", i);// 获取月份
				ChartSales sales = chartService.querySumMoney(map);
				Objective tive = objectiveService.getInvite(map);
				if (sales != null) {
					chartSales.add(String.format("%.2f", (sales.getSumMoney() / 10000)));
				} else {
					chartSales.add("0");
				}
				if (tive != null) {
					objective.add(String.format("%.2f", (tive.getObjectiveMoney() / 10000)));
				} else {
					objective.add("0");
				}
			}
			json.put("chartSales", chartSales);
			json.put("objective", objective);
			json.put("code", 0);
		} catch (Exception e) {
			json.put("code", -1);
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}

	@RequestMapping("checkSetTarget.do")
	@ResponseBody
	public Map<String, Object> checkSetTarget(String id,HttpServletRequest request, HttpSession session) {// 检查是否设置目标
		Map<String, Object> json = new HashMap<String, Object>(); // 定义返回map集合
		Map<String, Object> map = new HashMap<String, Object>(); // 定义查询条件map集合
		Objective objective = null;
		try {
			map.put("objectiveId",id);
			objective = objectiveService.getInvite(map);
			if (objective != null) {
				json.put("code", -2);
			} else {
				json.put("code", 0);
			}
		} catch (Exception e) {
			json.put("code", -1);
			logger.error("获取数据失败，原因：" + e);
		}

		return json;
	}

	@RequestMapping("toMarketingObjectives.do")
	public String toMarketingObjectives(HttpServletRequest request, HttpSession session) {// 跳转销售目标页面
		Map<String, Object> map = new HashMap<>();
		Employee user = (Employee) session.getAttribute("loginUser");
		map.put("canNotReadAllMessage", false);
		List<Department> list = new ArrayList<Department>();
		List<Department> listDept = departmentService.getDepartmentList(map);
		if (user.getQuarters().equals("3") || user.getQuarters().equals("7")) {
			map.put("employeeId", user.getEmployeeId());
			List<Pluralize> pluralizeList = pluralizeService.getPluralizeList(map);
			if (user.getQuarters().equals("3")) {
				list.add(user.getDept());
			}
			for (Pluralize pluralize : pluralizeList) {
				for (Department dept : listDept) {
					if (!pluralize.getDeptId().equals(user.getDept().getDeptId())) {
						if (pluralize.getDeptId().equals(dept.getDeptId())) {
							list.add(dept);
						}
					}
				}
			}
		}
		request.setAttribute("dept", list);
		return "WEB-INF/jsp/chartStatistics/marketing_objectives";
	}
	@RequestMapping("getUserDept.do")
	@ResponseBody
	public Map<String, Object> getUserDept(HttpServletRequest request, HttpSession session) {// 检查是否设置目标
		Map<String, Object> json = new HashMap<String, Object>(); // 定义返回map集合
		try {
			Map<String, Object> map = new HashMap<>();
			Employee user = (Employee) session.getAttribute("loginUser");
			map.put("canNotReadAllMessage", false);
			List<Department> list = new ArrayList<Department>();
			List<Department> listDept = departmentService.getDepartmentList(map);
			if (user.getQuarters().equals("3") || user.getQuarters().equals("7")) {
				map.put("employeeId", user.getEmployeeId());
				List<Pluralize> pluralizeList = pluralizeService.getPluralizeList(map);
				if (user.getQuarters().equals("3")) {
					list.add(user.getDept());
				}
				for (Pluralize pluralize : pluralizeList) {
					for (Department dept : listDept) {
						if (!pluralize.getDeptId().equals(user.getDept().getDeptId())) {
							if (pluralize.getDeptId().equals(dept.getDeptId())) {
								list.add(dept);
							}
						}
					}
				}
			}
			json.put("data",list);
			json.put("code", 0);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", -1);
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}

	@RequestMapping("toDeptChart.do")
	public String toDeptChart(HttpServletRequest request) {
		return "WEB-INF/jsp/deptChart";
	}

	@RequestMapping("toTeamChart.do")
	public String toTeamChart(HttpServletRequest request, HttpSession session) {

		try {
			Employee user = (Employee) request.getSession().getAttribute("loginUser");
			Map<String, Object> map = new HashMap<>();
			// 设置权限，如果menuId中有44查看所有销售排名情况的，则有所有查看权限
			Boolean canNotReadAllMessage = true;
			List<Department> list = new ArrayList<Department>();
			map.put("canNotReadAllMessage", false);
			List<Department> listDept = departmentService.getDepartmentList(map);
			if (checkPower(session, 99)) {
				canNotReadAllMessage = false;
				Department dept = new Department();

				dept.setDeptName("全部分公司");
				dept.setRemake("滨海金控");
				list.add(dept);
				for (int i = 1; i <= listDept.size(); i++) {
					list.add(listDept.get(i - 1));
				}
			} else if (checkPower(session, 96)) {
				map.put("employeeId", user.getEmployeeId());
				List<Pluralize> pluralizeList = pluralizeService.getPluralizeList(map);
				if (!user.getQuarters().equals("7") && !user.getQuarters().equals("8")) {
					list.add(user.getDept());
				}
				for (Pluralize pluralize : pluralizeList) {
					for (Department dept : listDept) {
						if (!pluralize.getDeptId().equals(user.getDept().getDeptId())) {
							if (pluralize.getDeptId().equals(dept.getDeptId())) {
								list.add(dept);
							}
						}
					}
				}
				if (list.size() == 0) {
					Department dept2 = new Department();
					dept2.setDeptName("-暂无数据-");
					dept2.setDeptId("99658");
					list.add(dept2);
				}
			}
			request.setAttribute("dept", list);
			request.setAttribute("canNotReadAllMessage", canNotReadAllMessage);

			if (user.getDept() != null) {
				request.setAttribute("deptId", user.getDept().getDeptId());
				if (canNotReadAllMessage == true) {
					request.setAttribute("isCanMod", 1);
					map.put("isBranch", 1);
					map.put("deptId", user.getDept().getDeptId());
					List<Branch> branchList = branchService.getBranchAllList(map);
					request.setAttribute("branchList", branchList);
				}

			}
			if (user.getBranch() != null) {
				request.setAttribute("branchId", user.getBranch().getBranchId());
				if (canNotReadAllMessage == true) {
					map.put("isBranch", null);
					map.put("pid", user.getBranch().getBranchId());
					List<Branch> teamList = branchService.getBranchAllList(map);
					request.setAttribute("teamList", teamList);
					request.setAttribute("isCanMod", 2);
				}
			}
			if (user.getTeam() != null) {
				request.setAttribute("teamId", user.getTeam().getBranchId());
				if (canNotReadAllMessage == true) {
					request.setAttribute("isCanMod", 3);
				}
			}
			return "WEB-INF/jsp/teamChart";
		} catch (Exception e) {
			logger.error("获取数据失败，原因：" + e);
			return "WEB-INF/jsp/error/500";
		}

	}

	/**
	 * 跳转到销售排名页面
	 * 
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping("toPersonalChart.do")
	public String toPersonalChart(HttpServletRequest request, HttpSession session) {
		try {
			Employee user = (Employee) request.getSession().getAttribute("loginUser");
			Map<String, Object> map = new HashMap<>();
			// 设置权限，如果menuId中有45查看所有销售排名情况的，则有所有查看权限
			Boolean canNotReadAllMessage = true;
			List<Department> list = new ArrayList<Department>();
			map.put("canNotReadAllMessage", false);
			List<Department> listDept = departmentService.getDepartmentList(map);
			if (checkPower(session, 98)) {
				canNotReadAllMessage = false;

				Department dept = new Department();

				dept.setDeptName("全部分公司");
				dept.setRemake("滨海金控");
				list.add(dept);
				for (int i = 1; i <= listDept.size(); i++) {
					list.add(listDept.get(i - 1));
				}
			} else if (checkPower(session, 95)) {
				map.put("employeeId", user.getEmployeeId());
				List<Pluralize> pluralizeList = pluralizeService.getPluralizeList(map);
				if (!user.getQuarters().equals("7") && !user.getQuarters().equals("8")) {
					list.add(user.getDept());
				}
				for (Pluralize pluralize : pluralizeList) {
					for (Department dept : listDept) {
						if (!pluralize.getDeptId().equals(user.getDept().getDeptId())) {
							if (pluralize.getDeptId().equals(dept.getDeptId())) {
								list.add(dept);
							}
						}
					}
				}
				if (list.size() == 0) {
					Department dept2 = new Department();
					dept2.setDeptName("-暂无数据-");
					dept2.setDeptId("99658");
					list.add(dept2);
				}
			}
			request.setAttribute("dept", list);
			request.setAttribute("canNotReadAllMessage", canNotReadAllMessage);
			if (user.getDept() != null) {
				request.setAttribute("deptId", user.getDept().getDeptId());
				if (canNotReadAllMessage == true) {
					request.setAttribute("isCanMod", 1);
					map.put("isBranch", 1);
					map.put("deptId", user.getDept().getDeptId());
					List<Branch> branchList = branchService.getBranchAllList(map);
					request.setAttribute("branchList", branchList);
				}

			}
			if (user.getBranch() != null) {
				request.setAttribute("branchId", user.getBranch().getBranchId());
				if (canNotReadAllMessage == true) {
					map.put("isBranch", null);
					map.put("pid", user.getBranch().getBranchId());
					List<Branch> teamList = branchService.getBranchAllList(map);
					request.setAttribute("teamList", teamList);
					request.setAttribute("isCanMod", 2);
				}
			}
			if (user.getTeam() != null) {
				request.setAttribute("teamId", user.getTeam().getBranchId());
				if (canNotReadAllMessage == true) {
					request.setAttribute("isCanMod", 3);
				}
			}
			return "WEB-INF/jsp/rankChart";
		} catch (Exception e) {
			logger.error("获取数据失败，原因：" + e);
			return "WEB-INF/jsp/error/500";
		}

	}

	/**
	 * 客户增长jsp页面
	 * 
	 * @return
	 */
	@RequestMapping("toCustomerGrowth.do")
	public String toCustomerGrowth(HttpServletRequest request, HttpSession session) {
		if (checkPower(session, 94) || checkPower(session, 97)) {
			try {
//				Employee user = (Employee) request.getSession().getAttribute("loginUser");
//				Map<String, Object> map = new HashMap<>();
//				// 设置权限，如果menuId中有97查看所有销售排名情况的，则有所有查看权限
//				List<Department> list = new ArrayList<Department>();
//				map.put("canNotReadAllMessage", false);
//				List<Department> listDept = departmentService.getDepartmentList(map);
//				if (checkPower(session, 97)) {
//					Department dept = new Department();
//					dept.setDeptName("全部分公司");
//					dept.setRemake("滨海金控");
//					list.add(dept);
//					for (int i = 1; i <= listDept.size(); i++) {
//						list.add(listDept.get(i - 1));
//					}
//				} else if (checkPower(session, 94)) {
//					map.put("employeeId", user.getEmployeeId());
//					List<Pluralize> pluralizeList = pluralizeService.getPluralizeList(map);
//					if (!user.getQuarters().equals("7") && !user.getQuarters().equals("8")) {
//						list.add(user.getDept());
//					}
//					for (Pluralize pluralize : pluralizeList) {// 获取兼任部门
//						for (Department dept : listDept) {
//							if (!pluralize.getDeptId().equals(user.getDept().getDeptId())) {
//								if (pluralize.getDeptId().equals(dept.getDeptId())) {
//									list.add(dept);
//								}
//							}
//						}
//					}
//					if (list.size() == 0) {
//						Department dept2 = new Department();
//						dept2.setDeptName("-暂无数据-");
//						dept2.setDeptId("99658");
//						list.add(dept2);
//					}
//				}
//				request.setAttribute("dept", list);
			} catch (Exception e) {
				logger.error("获取数据失败，原因：" + e);
				return "WEB-INF/jsp/error/500";
			}
			return "WEB-INF/jsp/chartStatistics/customer_growth";
		} else {
			return "WEB-INF/jsp/error/404";
		}
	}

	/**
	 * 销售图表
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/saleChart.do")
	@ResponseBody
	public Map<String, Object> teamSaleChart(HttpServletRequest request, HttpSession session, String method,
			String date, String deptId, String branchId, String teamId, String empId, String quarter,
			Boolean canNotReadAllMessage) throws Exception {
		Map<String, Object> json = new HashMap<>();
		Employee user = (Employee) session.getAttribute("loginUser");// 当前登录对象
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("method", method);
			map.put("quarter", quarter);
			map.put("date", date);
			if (deptId != null && !deptId.equals("1")) {
				map.put("deptId", deptId);
			}
			map.put("branchId", branchId);
			map.put("teamId", teamId);
			map.put("empId", empId);

			// 选择部门下拉框，查询团队排名
			if (branchId != "" && branchId != null && canNotReadAllMessage != null && canNotReadAllMessage
					&& user.getTeam() != null && teamId == null) {
				return json;
			}
			// 选择分公司下拉框，查询部门排名
			else if (deptId != "" && deptId != null && canNotReadAllMessage != null && canNotReadAllMessage
					&& user.getBranch() != null && branchId == null) {
				return json;
			}

			// 设置默认登录查询的信息
			if (method == null || method == "") {
				// 拥有查询所有信息的权限
				if (canNotReadAllMessage != null && !canNotReadAllMessage) {
					map.put("method", "");
				}
				// 如果登录用户有团队id，说明是团队长，默认查询团队中员工的排名
				else if (user.getTeam() != null) {
					map.put("method", "team");
					map.put("teamId", user.getTeam().getBranchId());
					method = "team";
				}
				// 如果登录用户没有有团队id，但拥有部门id，说明是部门经理，默认查询部门中团队的排名
				else if (user.getBranch() != null) {
					map.put("method", "branch");
					map.put("branchId", user.getBranch().getBranchId());
					method = "branch";
				}
				// 如果登录用户没有有团队id和部门id，但拥有分公司id，说明是分公司经理，默认查询所在分公司中部门的排名
				else if (user.getDept() != null) {
					map.put("method", "dept");
					map.put("deptId", user.getDept().getDeptId());
					method = "dept";
				}
			}
			List<ChartSales> list = chartService.getChartSaleList(map);
			String dept[] = new String[list.size()];
			String countSale[] = new String[list.size()];
			Integer index = 0;
			double totality = 0;
			List<Map<String, Object>> data = new ArrayList<>();
			NumberFormat nf = new DecimalFormat("#,###.##");
			for (ChartSales chart : list) {
				Map<String, Object> dataMap = new LinkedHashMap<>();
				if (method == null || method == "") {
					dept[index] = chart.getDept().getDeptName();
					dataMap.put("name", chart.getDept().getDeptName());
				} else if ("branch".equals(method)) {
					if (chart.getTeam() != null) {
						dept[index] = chart.getTeam().getBranchName();
						dataMap.put("name", chart.getTeam().getBranchName());
					} else {
						break;
					}
				} else if ("dept".equals(method)) {
					dept[index] = chart.getBranch().getBranchName();
					dataMap.put("name", chart.getBranch().getBranchName());
				} else if ("team".equals(method) || "emp".equals(method)) {
					dept[index] = chart.getEmpName();
					dataMap.put("name", chart.getEmpName());
				}
				countSale[index] = chart.getCountSalesAmount().toString();
				dataMap.put("value", chart.getCountSalesAmount());
				dataMap.put("values", nf.format(chart.getCountSalesAmount()));
				totality += chart.getCountSalesAmount();// 总数
				// dataMap.put("name", chart.getDept().getDeptName());
				data.add(dataMap);
				index++;
			}
			json.put("dept", dept);
			json.put("data", data);
			json.put("totality", nf.format(totality));
			json.put("countSale", countSale);
			json.put("code", 1);
		} catch (Exception e) {
			json.put("code", -1);
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}

	/**
	 * 销售前十图表
	 * 
	 * @param request
	 * @param session
	 * @param method
	 *            查看类型（默认为emp）
	 * @param date
	 *            查看时间类型
	 * @param deptOption
	 *            分公司id
	 * @param branchOption
	 *            部门id
	 * @param teamOption
	 *            团队id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/rankSaleChart.do")
	@ResponseBody
	public Map<String, Object> rankSaleChart(HttpServletRequest request, HttpSession session, String method,
			String date, String deptOption, String branchOption, String teamOption) throws Exception {
		Map<String, Object> json = new HashMap<>();
		Map<String, Object> returnMap = new HashMap<>();
		Employee user = (Employee) session.getAttribute("loginUser");// 当前登录对象
		int isBranchTeamEmp = 0;
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("method", method);
			map.put("date", date);
			if (deptOption != null && !deptOption.equals("1")) {
				map.put("deptId", deptOption);
			}
			map.put("branchId", branchOption);
			map.put("teamId", teamOption);

			// 选择团队下拉框，查询员工排名
			if (teamOption != null && teamOption.length() != 0) {
				map.put("method", method);
				isBranchTeamEmp = 1;
			}
			// 选择部门下拉框，查询团队排名
			else if (branchOption != null && branchOption.length() != 0) {
				if (!checkPower(session, 95) && user.getTeam() != null) {
					return json;
				} else {
					map.put("method", "team");
					isBranchTeamEmp = 2;
				}
			}
			// 选择分公司下拉框，查询部门排名
			else if (deptOption != null && deptOption.length() != 0) {
				if (!checkPower(session, 95) && user.getBranch() != null) {
					return json;
				} else {
					map.put("method", "branch");
					isBranchTeamEmp = 3;
				}

			}
			// 登录，没有选择任何下拉框
			else {
				if (checkPower(session, 98)) {
					map.put("canNotReadAllMessage", false);
					map.put("method", "dept");
					isBranchTeamEmp = 0;
				}
				// 如果登录用户有团队id，说明是团队长，默认查询团队中员工的排名
				else if (user.getTeam() != null) {
					map.put("teamOption", user.getTeam().getBranchId());
					map.put("method", "emp");
					map.put("teamId", user.getTeam().getBranchId());
					isBranchTeamEmp = 1;
				}
				// 如果登录用户没有有团队id，但拥有部门id，说明是部门经理，默认查询部门中团队的排名
				else if (user.getBranch() != null) {
					map.put("branchOption", user.getBranch().getBranchId());
					map.put("method", "team");
					map.put("branchId", user.getBranch().getBranchId());
					isBranchTeamEmp = 2;
				}
				// 如果登录用户没有有团队id和部门id，但拥有分公司id，说明是分公司经理，默认查询所在分公司中部门的排名
				else if (user.getDept() != null) {
					map.put("deptOption", user.getDept().getDeptId());
					map.put("method", "branch");
					map.put("deptId", user.getDept().getDeptId());
					isBranchTeamEmp = 3;
				}
			}
			// 用于判断是查询哪个级别的排名
			map.put("isBranchTeamEmp", isBranchTeamEmp);
			// 执行 查询
			List<ChartSales> list = chartService.getRankChartList(map);
			String emp[] = new String[list.size()];
			String dept[] = new String[list.size()];
			String countSale[] = new String[list.size()];
			Integer index = 0;
			List<Map<String, Object>> data = new ArrayList<>();
			List<Map<String, Object>> reuturnlist = new ArrayList<>();
			NumberFormat nf = new DecimalFormat("#,###.##");
			double totality = 0;
			for (ChartSales chart : list) {
				Map<String, Object> dataMap = new LinkedHashMap<>();
				// 判断统计了什么的销售量排名
				if (isBranchTeamEmp == 0) {
					emp[index] = chart.getDept().getDeptName();
					returnMap = new HashMap<>();
					returnMap.put("name", chart.getDept().getDeptName());
					if (chart.getCountSalesAmount()!=null) {
						returnMap.put("countSalesAmount", chart.getCountSalesAmount());
					}else{
						returnMap.put("countSalesAmount", "0");
					}
					
					dataMap.put("name", chart.getDept().getDeptName());
				} else if (isBranchTeamEmp == 1) {
					emp[index] = chart.getEmpName();
					returnMap = new HashMap<>();
					if (chart.getCountSalesAmount()!=null) {
						returnMap.put("countSalesAmount", chart.getCountSalesAmount());
					}else{
						returnMap.put("countSalesAmount", "0");
					}
					returnMap.put("name", chart.getEmpName());
					dataMap.put("name", chart.getEmpName());
				} else if (isBranchTeamEmp == 2) {
					emp[index] = chart.getBranch().getBranchName();
					returnMap = new HashMap<>();
					if (chart.getCountSalesAmount()!=null) {
						returnMap.put("countSalesAmount", chart.getCountSalesAmount());
					}else{
						returnMap.put("countSalesAmount", "0");
					}
					returnMap.put("name", chart.getBranch().getBranchName());
					dataMap.put("name", chart.getBranch().getBranchName());
				} else if (isBranchTeamEmp == 3) {
					emp[index] = chart.getBranch().getBranchName();
					returnMap = new HashMap<>();
					if (chart.getCountSalesAmount()!=null) {
						returnMap.put("countSalesAmount", chart.getCountSalesAmount());
					}else{
						returnMap.put("countSalesAmount", "0");
					}
					returnMap.put("name", chart.getBranch().getBranchName());
					dataMap.put("name", chart.getBranch().getBranchName());
				}
				if (chart.getCountSalesAmount()!=null) {
					countSale[index] = chart.getCountSalesAmount().toString();
					dataMap.put("value", chart.getCountSalesAmount());
					dataMap.put("values", nf.format(chart.getCountSalesAmount()));
					totality += chart.getCountSalesAmount();
				}else {
					countSale[index] = "0";
					dataMap.put("value", "0");
					dataMap.put("values", "0");
				}
				
				reuturnlist.add(returnMap);
				data.add(dataMap);
				index++;
			}
			json.put("totality", nf.format(totality));
			json.put("emp", emp);
			json.put("dept", dept);
			json.put("data", data);
			json.put("list", reuturnlist);
			json.put("countSale", countSale);
			json.put("code", 1);
		} catch (Exception e) {
			json.put("code", -1);
			logger.error("获取数据失败，原因：" + e);
			e.printStackTrace();
		}
		return json;
	}

	/**
	 * 客户数统计各部门/各团队
	 * 
	 * @param request
	 * @param session
	 * @param dateType
	 *            日期查询类型
	 * @param deptType
	 *            部门查询类型
	 * @return
	 */
	@RequestMapping("/customerCharts.do")
	@ResponseBody
	public Map<String, Object> getCustomerCharts(HttpServletRequest request, HttpSession session, String dateType,
			String type, String deptId, String branchId, String teamId) {
		Map<String, Object> json = new HashMap<String, Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		Employee user = (Employee) request.getSession().getAttribute("loginUser");
		// 设置权限，如果menuId中有46查看所有销售排名情况的，则有所有查看权限
		Boolean canNotReadAllMessage = true;
		if (checkPower(session, 97)) {
			canNotReadAllMessage = false;
		}
		try {
			// 查询参数
			map.put("type", type);
			map.put("deptId", deptId);
			map.put("branchId", branchId);
			map.put("teamId", teamId);
			map.put("dateType", dateType);
			if (dateType == null || "".equals(dateType)) {
				map.put("dateType", "nowYear");
			}
			if (canNotReadAllMessage) {
				map.put("isDeptUser", "1");
				map.put("deptId", user.getDept().getDeptId());
			} else {
				map.put("isDeptUser", null);
			}
			List<CustomerGrowth> customerGrowthList = chartService.getCustomerGrowthChart(map);
			List<Map<String, Object>> datas = new ArrayList<Map<String, Object>>();
			String max[] = new String[customerGrowthList.size()];
			String dept[] = new String[customerGrowthList.size()];
			String personNumbers[] = new String[customerGrowthList.size()];
			Integer index = 0;
			Integer customerCount = 0;
			for (CustomerGrowth increatNum : customerGrowthList) {
				if (increatNum.getDeptName() == null || increatNum.getDeptName() == null) {
					continue;
				}
				dept[index] = increatNum.getDeptName();
				personNumbers[index] = increatNum.getPersonNumber().toString();
				index++;
				Map<String, Object> dataMap = new HashMap<String, Object>();
				dataMap.put("name", increatNum.getDeptName());
				dataMap.put("value", increatNum.getPersonNumber());
				datas.add(dataMap);
				customerCount += increatNum.getPersonNumber();
			}
			json.put("data", customerGrowthList);
			json.put("datas", datas);
			json.put("dept", dept);
			json.put("max", max);
			json.put("customerCount", customerCount);
			json.put("personNumbers", personNumbers);
		} catch (Exception e) {
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}
}
